﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace habbo.Kernel.Commons
{
    class Configuration
    {
        private static Configuration _Instance;

        public Dictionary<string, string> configData;

        public Configuration(string Path)
        {
            configData = new Dictionary<string, string>();
            if (!File.Exists(Path))
            {
                SystemApp.ConsoleSystem.PrintLine("ERROR", "Unable to locate the configuration file at {0}", Path);
                return;
            }

            try
            {
                using (StreamReader sr = new StreamReader(Path))
                {
                    string line = null;

                    while ((line = sr.ReadLine()) != null)
                    {
                        if(line.Length < 1 || line.StartsWith("#"))
                            continue;

                        if (line.Contains("="))
                        {
                            var Key = line.Split('=')[0];
                            var Value = line.Split('=')[1];

                            configData.Add(Key, Value);
                        }
                    }

                    sr.Dispose();
                    sr.Close();
                }
            }
            catch (Exception e)
            {
                throw new Exception("Could not process configuration file: " + e.ToString());
            }
        }

        public static Configuration getConfig(string file = "")
        {
            if (_Instance == null)
            {
                _Instance = new Configuration(file);
            }
            return _Instance;
        }
    }
}
